package com.wang.controller;


import com.wang.base.http.RestResponse;
import com.wang.base.util.TokenUtil;
import com.wang.domain.TongueDiagnosis;
import com.wang.domain.TonguePic;
import com.wang.domain.TonguePicItem;
import com.wang.domain.User;
import com.wang.model.Mark.MarkTongueVM;
import com.wang.service.TongueDiagnosisService;
import com.wang.service.TonguePicItemService;
import com.wang.service.TonguePicService;
import com.wang.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WangZai
 * @since 2023-04-27
 */
@CrossOrigin
@RestController
@RequestMapping("/td/api/tonguePic")
@Api(tags = "舌诊标注模块接口说明")
public class TonguePicController {
    private final ModelMapper modelMapper;
    private final UserService userService;
    private final TonguePicService tonguePicService;
    private final TonguePicItemService tonguePicItemService;

    private static final Logger log = LoggerFactory.getLogger(TongueDiagnosisController.class);

    @Autowired

    public TonguePicController(ModelMapper modelMapper, UserService userService, TonguePicService tonguePicService, TonguePicItemService tonguePicItemService) {
        this.modelMapper = modelMapper;
        this.userService = userService;
        this.tonguePicService = tonguePicService;
        this.tonguePicItemService = tonguePicItemService;
    }


    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/saveTonguePic")
    @ApiOperation("上传单图片接口")
    public RestResponse<Object> saveTonguePic(@RequestParam("file") MultipartFile file) {
        String uuid = UUID.randomUUID().toString();
        try {
            byte[] bytes = file.getBytes();
            TonguePicItem tonguePicItem = new TonguePicItem();
            tonguePicItem.setUUID(uuid);
            tonguePicItem.setPicData(bytes);
            log.info("图片保存结果：" + tonguePicItemService.saveTonguePicItem(tonguePicItem));
        } catch (IOException e) {
            e.printStackTrace();
        }
        TonguePicItem tonguePicItem = tonguePicItemService.getTonguePicItemByUUID(uuid);
        TonguePic tonguePic = new TonguePic();
        tonguePic.setTonguePicItemId(tonguePicItem.getTonguePicItemId());
        if (tonguePicService.saveTonguePic(tonguePic)) {
            return RestResponse.ok("添加成功！");
        } else {
            return RestResponse.failure("添加失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/saveTonguePicList")
    @ApiOperation("上传多张图片接口")
    public RestResponse<Object> saveTonguePicList(@RequestParam("files") MultipartFile[] files) {
        List<TonguePic> tonguePicList = new ArrayList<>();
        List<TonguePicItem> tonguePicItemList = new ArrayList<>();
        for(MultipartFile file : files) {
            String uuid = UUID.randomUUID().toString();
            try {
                byte[] bytes = file.getBytes();
                TonguePicItem tonguePicItem = new TonguePicItem();
                tonguePicItem.setUUID(uuid);
                tonguePicItem.setPicData(bytes);
                tonguePicItemService.saveTonguePicItem(tonguePicItem);
            } catch (IOException e) {
                e.printStackTrace();
            }
            TonguePicItem tonguePicItem = tonguePicItemService.getTonguePicItemByUUID(uuid);
            TonguePic tonguePic = new TonguePic();
            tonguePic.setTonguePicItemId(tonguePicItem.getTonguePicItemId());
            tonguePicList.add(tonguePic);
        }
        if (tonguePicService.saveTonguePicList(tonguePicList)) {
            return RestResponse.ok("添加成功！");
        } else {
            return RestResponse.failure("添加失败");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/selectTonguePicList")
    @ApiOperation("获取未标注图片接口")
    public RestResponse<Object> selectTonguePicList() {
        List<TonguePic> tonguePicList = tonguePicService.getTonguePicList();
        List<TonguePic> tonguePics = new ArrayList<>();
        for (TonguePic tonguePic : tonguePicList) {
            if (tonguePic.getCoated() == null || tonguePic.getColor() == null || tonguePic.getCrack() == null || tonguePic.getIndentation() == null) {
                tonguePics.add(tonguePic);
            }
        }
        if (tonguePics.isEmpty()) {
            return RestResponse.failure("暂无未标注图片！！！");
        } else {
            return RestResponse.ok(tonguePics);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/selectDealPicList")
    @ApiOperation("获取已标注图片接口")
    public RestResponse<Object> selectDealPicList() {
        List<TonguePic> tonguePicList = tonguePicService.getTonguePicList();
        List<TonguePic> tonguePics = new ArrayList<>();
        for (TonguePic tonguePic : tonguePicList) {
            if (tonguePic.getCoated() != null || tonguePic.getColor() != null || tonguePic.getCrack() != null || tonguePic.getIndentation() != null) {
                tonguePics.add(tonguePic);
            }
        }
        if (tonguePics.isEmpty()) {
            return RestResponse.failure("暂无已标注图片！！！");
        } else {
            return RestResponse.ok(tonguePics);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/selectAllPic")
    @ApiOperation("获取所有标注图片接口")
    public RestResponse<Object> selectAllPic() {
        List<TonguePic> tonguePicList = tonguePicService.getTonguePicList();
        if (tonguePicList.isEmpty()) {
            return RestResponse.failure("暂无相关图片！！！");
        } else {
            return RestResponse.ok(tonguePicList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/markTonguePic")
    @ApiOperation("标注图片接口")
    public RestResponse<Object> markTonguePic(@RequestBody MarkTongueVM model) {
        TonguePic tonguePic = tonguePicService.getTonguePicById(model.getTonguePicId());
        tonguePic.setCoated(model.getCoated());
        tonguePic.setColor(model.getColor());
        tonguePic.setCrack(model.getCrack());
        tonguePic.setIndentation(model.getIndentation());
        if (tonguePicService.updateTonguePic(tonguePic)) {
            return RestResponse.ok("标注成功！");
        } else {
            return RestResponse.failure("标注失败！！！");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @GetMapping(value = "/deleteTonguePic/{tonguePicId}")
    @ApiOperation("删除标注图片接口")
    public RestResponse<Object> deleteTonguePic(@PathVariable Integer tonguePicId) {
        TonguePic tonguePic = tonguePicService.getTonguePicById(tonguePicId);
        if(tonguePicService.deleteTonguePic(tonguePicId)&&tonguePicItemService.deleteTonguePicItem(tonguePic.getTonguePicItemId())){
            return RestResponse.ok("删除成功");
        }else {
            return RestResponse.failure("删除失败！！！");
        }
    }
}

